package com.ruoyi.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.mobile.domain.SysUserAddressEntity;
import com.ruoyi.mobile.mapper.ISysUserAddressMapper;
import com.ruoyi.mobile.service.ISysUserAddressService;
import com.ruoyi.mobile.utils.NftResponse;
import com.ruoyi.mobile.utils.QueryUtil;
import com.ruoyi.mobile.utils.ResponseConstants;
import com.ruoyi.system.api.req.SysUserAddressReq;
import com.ruoyi.system.api.resp.SysUserAddressResp;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户收货地址信息Service业务层处理
 */
@Service
@Transactional
public class SysUserAddressServiceImpl extends ServiceImpl<ISysUserAddressMapper, SysUserAddressEntity> implements ISysUserAddressService {

    @Override
    public NftResponse<List<SysUserAddressResp>> getAddressList(SysUserAddressReq query) {
        QueryWrapper<SysUserAddressEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(QueryUtil.camelToUnderline("userId"), query.getUserId());
        wrapper.orderByDesc("isfirst");
        List<SysUserAddressEntity> list = this.baseMapper.selectList(wrapper);
        List<SysUserAddressResp> rtnList = BeanUtil.copyToList(list, SysUserAddressResp.class);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", rtnList);
    }

    @Override
    public NftResponse<SysUserAddressResp> getAddrInfo(Long id) {
        SysUserAddressEntity entity = this.baseMapper.selectById(id);
        SysUserAddressResp rtn = BeanUtil.copyProperties(entity, SysUserAddressResp.class);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", rtn);
    }

    @Override
    public NftResponse<Boolean> insertAddressInfo(SysUserAddressReq query) {
        if (null == query.getUserId() || query.getUserId() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入用户信息", false);
        }
        if (null == query.getName() || "".equals(query.getName())) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入收货人姓名", false);
        }
        if (null == query.getPhone() || "".equals(query.getPhone())) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入收货人电话", false);
        }
        if (null == query.getAddress() || "".equals(query.getAddress())) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入收货详细地址", false);
        }
        SysUserAddressEntity entity = BeanUtil.copyProperties(query, SysUserAddressEntity.class);
        this.baseMapper.insert(entity);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "添加成功", true);
    }

    @Override
    public NftResponse<Boolean> updateAddressInfo(SysUserAddressReq query) {
        if (null == query.getId() || query.getId() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入地址主键信息", false);
        }
        if (query.getIsfirst() > 0) {
            QueryWrapper<SysUserAddressEntity> wrapper = new QueryWrapper<>();
            wrapper.eq(QueryUtil.camelToUnderline("userId"), query.getUserId());
            List<SysUserAddressEntity> list = this.baseMapper.selectList(wrapper);
            for (SysUserAddressEntity entity:list) {
                if (entity.getId()==query.getId()) {
                    entity.setIsfirst(1);
                }else {
                    entity.setIsfirst(0);
                }
                QueryWrapper<SysUserAddressEntity> wrapperQ = new QueryWrapper<>();
                wrapperQ.eq(QueryUtil.camelToUnderline("id"), entity.getId());
                this.baseMapper.update(entity,wrapperQ);
            }
        }else {
            if (null == query.getName() || "".equals(query.getName())) {
                return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入收货人姓名", false);
            }
            if (null == query.getPhone() || "".equals(query.getPhone())) {
                return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入收货人电话", false);
            }
            if (null == query.getAddress() || "".equals(query.getAddress())) {
                return new NftResponse<>(ResponseConstants.ERROR_CODE, "请输入收货详细地址", false);
            }
            SysUserAddressEntity entity = BeanUtil.copyProperties(query, SysUserAddressEntity.class);
            QueryWrapper<SysUserAddressEntity> wrapper = new QueryWrapper<>();
            wrapper.eq(QueryUtil.camelToUnderline("id"), query.getId());
            this.baseMapper.update(entity,wrapper);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "修改成功", true);
    }

    @Override
    public NftResponse<Boolean> deleteAddressInfo(SysUserAddressReq query) {
        QueryWrapper<SysUserAddressEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(QueryUtil.camelToUnderline("id"), query.getId());
        this.baseMapper.delete(wrapper);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "删除成功", true);
    }
}
